
#include <string.h>
#include "BSP.h"
//	#include "Driver.h"
#include "BSP_Timer.h"
#include "Timer.h"
#include <log.h>
#include "FreeRTOS.h"
#include "Task.h"
#include "Timers.h"
#include "task_config.h"

#include "SimpleTag.h"

#include <Reader/Reader.h>
#include <BLE/BLE.h>

#define NO_PROC()		((void)0)
#define NO_VAL		

#define CHECK_RETURN(cond, val, proc)	do{if(cond){proc; return val;}}while(0)


#if 1
int BLE_FindGattServerAttribute(uint16_t * attrhandle);
int BLE_RequestEndpointClose(void);
int BLE_GetGattServerAttributeValue(uint16_t AttrHandle, uint8_t* data, uint8_t* len);

static void Reader_BEL_Tick(TimerHandle_t xTimer){
	ReaderBLE_T* ble = &reader.ble;
	if(ble->life > 0){
		ble->life--;
	}
	if(ble->serviceCount > 0){
		ble->serviceCount--;
	}
	if(ble->checkCount > 0){
		ble->checkCount--;
	}
}

int Reader_BLE_Read(SimpleTag_T* tag){
	ReaderBLE_T* ble = &reader.ble;
	
	if(ble->life < 1){
		return -2;
	}
	
	memcpy(tag->uid, ble->data, ble->length);
	tag->ulen = ble->length;
	return 0;
}


static void Reader_BLE_Run(ReaderBLE_T* ble){
	int ret = -1;

	ret = BLE_GetGattServerAttributeValue(ble->handlerCard, ble->data, &ble->length);
	if(!ret){
		ble->life = 3;
	}
}


void Reader_BLE_Service(void){
	int ret;
	ReaderBLE_T* ble = &reader.ble;
	
	if(ble->serviceCount){
		return ;
	}
	ble->serviceCount = 2;
	
	if(reader.hfFlags & TAG_MASK_BLE){
		if(!ble->state){
			ret = BLE_Init(1);
			CHECK_RETURN(ret, NO_VAL, TRACE_W("BLE_Init fail(%d)", ret));
		}
		
		if(!ble->handlerCard){
			ret = BLE_FindGattServerAttribute(&ble->handlerCard);
			CHECK_RETURN(ret, NO_VAL, TRACE_W("BLE_FindGattServerAttribute fail(%d)", ret));
		}
	}
	else{
		if(ble->handlerCard){
			BLE_RequestEndpointClose();
			ble->handlerCard = 0;
		}
	}
	
	if(ble->handlerCard){
		Reader_BLE_Run(ble);
	}

	if(ble->handlerCard || ble->flag){
		//蓝牙处于开启状态
		if(ble->checkCount > 0){
			return ;
		}
		ble->checkCount = 5;
		uint8_t state = 0;
		ret = BLE_GetState(&state);
		CHECK_RETURN(ret, NO_VAL, NO_PROC());
		if(!state){
			//蓝牙异常关闭，重新开启
			ret = BLE_Init(1);
			CHECK_RETURN(ret, NO_VAL, TRACE_W("BLE_Init fail(%d)", ret));
			if(ble->handlerCard){
				ret = BLE_FindGattServerAttribute(&ble->handlerCard);
				CHECK_RETURN(ret, NO_VAL, TRACE_W("BLE_FindGattServerAttribute fail(%d)", ret));
			}
		}
	}
	else{
		//蓝牙处于关闭状态
		if(ble->state){
			ret = BLE_Init(0);
			CHECK_RETURN(ret, NO_VAL, TRACE_W("BLE_Init fail(%d)", ret));
		}
	}
}
#endif 


static int BLE_Check(void){
	int i;
	int ret = 0;
	uint8_t state = 0;
	
	for(i = 0; i < 3; i++){
		if(!BLE_GetState(&state)){
			ret = 1;
			break;
		}
		vTaskDelay(50);
	}
	
	return ret;
}

//设置MCU版本信息
void GetMCUInfo(MCU_Info_T* info);

static int MCU_Info_Cmp(const MCU_Info_T* info1, const MCU_Info_T* info2){
	if(strcmp((char*)info1->ver, (char*)info2->ver)){
		return 1;
	}

	if(strcmp((char*)info1->Model, (char*)info2->Model)){
		return 1;
	}
	
	return 0;
}

static void BLE_Setup(void){
	MCU_Info_T info1, info2;
	int i;

	for(i = 0; i < 3; i++){
		if(!BLE_GetMCUInfo(&info2)){
			break;
		}
		vTaskDelay(50);
	}

	if(i == 3){
		return ;
	}
	
	GetMCUInfo(&info1);
	if(!MCU_Info_Cmp(&info1, &info2)){
		return ;
	}
	
	for(i = 0; i < 3; i++){
		if(!BLE_SetMCUInfo(&info1)){
			break;
		}
		vTaskDelay(50);
	}
	//重启生效
	BLE_Reset();
}

void Reader_BLE_Init(void){
	if(BLE_Check()){
		//蓝牙串口连接正常
		BLE_Setup();
		vTaskDelay(10);
		BLE_Init(0);
		reader.attr |=RD_ATTR_BT;
	}
	else{
		reader.attr &= ~RD_ATTR_BT;
	}
	
#if 0
	reader.ble.handlerTimer = (void*)xTimerCreate("ble", 
		pdMS_TO_TICKS(100), pdTRUE, NULL, Reader_BEL_Tick);
	
	xTimerStart(reader.ble.handlerTimer, portMAX_DELAY);
#endif 
}


